﻿#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include "glad/glad.h"

#include <cstring>
#include <iostream>
#include <string>
#include <vector>

struct ScreenQuad {
    GLuint VAO, VBO;

    void initialize() {
        float vertices[] = {
            // 位置       // 纹理坐标
            -1.0f,  1.0f, 0.0f, 1.0f,
            -1.0f, -1.0f, 0.0f, 0.0f,
             1.0f, -1.0f, 1.0f, 0.0f,
            -1.0f,  1.0f, 0.0f, 1.0f,
             1.0f, -1.0f, 1.0f, 0.0f,
             1.0f,  1.0f, 1.0f, 1.0f
        };

        glGenVertexArrays(1, &VAO);
        glGenBuffers(1, &VBO);

        glBindVertexArray(VAO);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);

        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
        glEnableVertexAttribArray(1);

        glBindVertexArray(0);
    }
};

static ScreenQuad screenQuad = {
    .VAO = 0,
    .VBO = 0
};

struct FrameWriter {
	GLuint fbo;
	GLuint texture;
	GLuint pbo;
	GLuint shaderProgram;
	int width;
	int height;
};

namespace Utils {
	GLuint activateShader(std::string vertexShaderPath, std::string fragmentShaderPath);
	void clearFrame(GLint fbo = 0);
	uint8_t* getFrame(FrameWriter writer, GLuint fbo, GLuint texture);
	FrameWriter initFrameWriter(int width, int height);
}
